package com.test.redis;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * spring redis 工具类
 *
 * @author xpp
 **/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisSetCache {
    @Resource
    public RedisTemplate redisTemplate;

    /**
     * 设置有效时间
     *
     * @param key     Redis键
     * @param timeout 超时时间
     * @return true=设置成功；false=设置失败
     */
    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 设置有效时间
     *
     * @param key     Redis键
     * @param timeout 超时时间
     * @param unit    时间单位
     * @return true=设置成功；false=设置失败
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 存储Hash数据
     *
     * @param key     Redis的key
     * @param hashKey Hash的key
     * @param value   Hash的value
     */
    public void setSet(String key, Set<Object> values) {
        redisTemplate.opsForSet().add(key, values.toArray());
    }
    /**
     * set中增加元素，支持一次增加多个元素，逗号分隔即可，结果返回添加的个数
     *
     * @param key
     * @param value
     * @return
     */
    public Long addSet(String key, Object... value) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().add(key, value);
        } catch (Exception e) {
             return size;
        }
        return size;
    }

    public Set<Object> getSet(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * set中移除指定元素
     *
     * @param key
     * @param value
     * @return
     */
    public Long removeSet(String key, Object value) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().remove(key, value);
        } catch (Exception e) {
            return size;
        }
        return size;
    }

    /**
     * 计算set集合大小
     *
     * @param key
     * @return
     */
    public Long countSet(String key) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 判断set中是否存在某元素
     *
     * @param key
     * @param value
     * @return
     */
    public Boolean hasMemberSet(String key, Object value) {
        Boolean exist = false;
        try {
            exist = redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return exist;
    }

    /**
     * 随机获取set中的一个元素
     *
     * @param key
     * @return
     */
    public Object randomMember(String key) {
        Object o = null;
        try {
            o = redisTemplate.opsForSet().randomMember(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 随机获取指定个数的元素（可能重复）
     *
     * @param key
     * @param num
     * @return
     */
    public List<Object> randomMembers(String key, Integer num) {
        List<Object> o = null;
        try {
            o = redisTemplate.opsForSet().randomMembers(key, num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 随机获取集合内指定个数的元素（不重复）
     *
     * @param key
     * @param num
     * @return
     */
    public Set<Object> distinctRandomMembers(String key, Integer num) {
        Set<Object> o = null;
        try {
            o = redisTemplate.opsForSet().distinctRandomMembers(key, num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 获取key下的所有元素
     *
     * @param key
     * @return
     */
    public Set<Object> members(String key) {
        Set<Object> result = null;
        try {
            result = redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将key1集合中的元素value移到key2集合中
     *
     * @param key1
     * @param value
     * @param key2
     * @return
     */
    public Boolean moveMember(String key1, Object value, String key2) {
        Boolean result = false;
        try {
            result = redisTemplate.opsForSet().move(key1, value, key2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 两个key之间取交集
     *
     * @param key
     * @param key2
     * @return
     */
    public Set<Object> intersectMember(String key, String key2) {
        Set<Object> result = null;
        try {
            result = redisTemplate.opsForSet().intersect(key, key2);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return result;
    }

}
